
package com.kugou.game.sdk.protocol;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONObject;

import com.kugou.framework.base.LogUtil;
import com.kugou.framework.http.KGHttpClient;
import com.kugou.framework.http.ResponsePackage;
import com.kugou.game.sdk.api.common.User;
import com.kugou.game.sdk.core.UserManager;
import com.kugou.game.sdk.entity.CouponHelpInfo;
import com.kugou.game.sdk.entity.ResponseMessage;
import com.kugou.game.sdk.utils.ProtocolCmd;

/**
 * 描述:获取代金券帮助信息
 *
 * @author wcj
 * @since 2016年5月9日 上午10:41:47
 */
public class RequestCouponHelpInfoTask {

    private ArrayList<String> mCmdList = new ArrayList<String>();

    /**
     * 请求可用代金券信息
     * 
     * @param type 1：游戏 0：游币
     * @return
     */
    public CouponInfoHelpResponse request() {
        ArrayList<HashMap<String, Object>> requestParams = new ArrayList<HashMap<String, Object>>();

        HttpBaseRequestPackage req = new HttpBaseRequestPackage();
        CouponInfoHelpResponse respObj = new CouponInfoHelpResponse();
        ResponsePackage resp = new PatchInfoResponsePackage();
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("cmd", ProtocolCmd.CMD_GET_COUPON_HELP_LIST);
        requestParams.add(params);
        mCmdList.add(ProtocolCmd.CMD_GET_COUPON_HELP_LIST);

        User user = UserManager.getInstance().getCurrentUserInternal();
        String userName = user.getAccountName();
        req.setUserName(userName);
        req.setRequestParams(requestParams);
        try {
            KGHttpClient.request(req, resp);
            resp.getResponseData(respObj);
            return respObj;
        } catch (Exception e) {
        }
        return null;
    }

    class PatchInfoResponsePackage extends HttpBaseResponsePackage<CouponInfoHelpResponse> {

        @Override
        public void handleResponse(CouponInfoHelpResponse response, String jsonStr) {
            try {
                LogUtil.d("response", "RequestCouponHelpInfoTask = " + jsonStr);

                JSONArray responseArray = new JSONArray(jsonStr);
                int size = responseArray.length();
                for (int i = 0; i < size; i++) {
                    if (i >= mCmdList.size()) {
                        break;
                    }
                    JSONObject jsonResp = responseArray.getJSONObject(i);

                    String cmd = jsonResp.optString("cmd");
                    String code = jsonResp.getString("code");
                    String message = jsonResp.getString("message");
                    String prompt = jsonResp.optString("prompt");
                    response.setCode(code);
                    response.setMessage(message);
                    response.setPrompt(prompt);

                    if (response.isUseful() && cmd.equals(ProtocolCmd.CMD_GET_COUPON_HELP_LIST)) {
                        JSONArray data = jsonResp.optJSONArray("data");
                        if (data != null && data.length() > 0) {
                            ArrayList<CouponHelpInfo> coupons = new ArrayList<CouponHelpInfo>();
                            for (int j = 0; j < data.length(); j++) {
                                CouponHelpInfo coupon = new CouponHelpInfo();
                                coupon.setTitle((j + 1) + "."
                                        + data.optJSONObject(j).optString("question"));
                                coupon.setAnswer(data.optJSONObject(j).optString("answer"));
                                coupons.add(coupon);
                            }
                            response.setCouponInfos(coupons);
                        }

                        response.setOk(true);
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 响应消息
     */
    public static class CouponInfoHelpResponse extends ResponseMessage {
        ArrayList<CouponHelpInfo> couponInfos;

        public ArrayList<CouponHelpInfo> getCouponInfos() {
            return couponInfos;
        }

        public void setCouponInfos(ArrayList<CouponHelpInfo> couponInfos) {
            this.couponInfos = couponInfos;
        }

    }
}
